    // Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AudioManage, {soundList} from "../framework/AudioManage";
import Tool from "../framework/Tool";
import sceneMediator from "../framework/sceneMediator";
import GameConfig from "../framework/GameConfig";
import gameData from "./gameData";

const {ccclass, property} = cc._decorator;

export enum spVoice{
    normal = 0,
    sp1 = 1,
    sp2 = 2,
}

@ccclass
export default class gameControl extends cc.Component {

    @property({type: cc.Component.EventHandler, displayName: "触发事件"})
    toolEvent: cc.Component.EventHandler[] = [];

    @property({displayName: "开启提示点击事件"})
    openTitleClick: boolean = false;

    @property({displayName: "开启暂停游戏物件功能"})
    openPauseAction: boolean = false;

    @property({displayName: "无尽关卡"})
    isEndless: boolean = false;

    @property({displayName: "无尽关卡2"})
    isEndless2: boolean = false;

    @property({displayName: "无尽关卡3"})
    isEndless3: boolean = false;

    @property({displayName: "无尽关卡4"})
    isEndless4: boolean = false;

    @property({type : cc.Enum(spVoice),displayName : "播放特殊关卡音效"})
    curVoice : spVoice = spVoice.normal;

    @property({displayName : "不统计"})
    canStatistics : boolean = false;

    @property(cc.Node)
    showNode : cc.Node[] = [];

    isGameOver : boolean = false;

    onLoad() {
        cc.director.getPhysicsManager().enabled = true;
        cc.director.getPhysicsManager().debugDrawFlags = 0;
        this.setVoiceType();
        this.registerEvent();
        Tool.loadPrefab("prefabs/gameUI", (res) => {
            let view = cc.instantiate(res);
            view.parent = cc.find("Canvas");
        });
    }

    setVoiceType(){
        switch (this.curVoice) {
            case 0 :
                AudioManage.playLevelMusic(soundList.levelBG, false);
                break;
            case 1:
                AudioManage.playLevelMusic(soundList.vortex, false);
                AudioManage.playSFX(soundList.fireBurn1,true,false,1);
                break;
            case 2:
                AudioManage.playLevelMusic(soundList.vortex, false);
                break;
        }
    }

    closeVoiceType(){
        switch (this.curVoice) {
            case 0 :
                AudioManage.playLevelMusic(soundList.levelBG, true);
                break;
            case 1:
                AudioManage.playLevelMusic(soundList.vortex, true);
                AudioManage.playSFX(soundList.fireBurn1,true,true,1);
                break;
            case 2:
                AudioManage.playLevelMusic(soundList.vortex, true);
        }
    }

    registerEvent() {
        //按下按键的监听事件
        console.log("关卡",sceneMediator.getShowScene());
        cc.game.on("receiveKeyEvent", () => {
            this.pressKeyAction();
        });
        cc.game.on("gameWin", () => {
            if(this.isGameOver) return;
            this.isGameOver = true;
            cc.game.emit("CloseShowCheats");
            cc.game.emit("gameOver");

            if (this.openPauseAction) {
                cc.game.emit("closePauseView");
            }

            console.log("通关成功");
            // AudioManage.playLevelMusic(soundList.levelBG, true);
            this.closeVoiceType();

            AudioManage.playSFX(soundList.victory);
            cc.game.emit("shieldPressKey", true);
            sceneMediator.saveLevelData();
            Tool.loadPrefab("prefabs/gameWin", (res) => {
                let view = cc.instantiate(res);
                view.parent = cc.find("Canvas");
            })
        });

        cc.game.on("gameLose", (res?) => {
            if(this.isGameOver) return;
            this.isGameOver = true;

            cc.game.emit("CloseShowCheats");

            if (res != 1) {
                cc.game.emit("gameOver");
                cc.game.emit("shieldPressKey", true);
            }
            if (this.openPauseAction) {
                cc.game.emit("closePauseView");
            }
            // AudioManage.playLevelMusic(soundList.levelBG, true);
            this.closeVoiceType();

            Tool.loadPrefab("prefabs/reliveView", (res) => {
                let view = cc.instantiate(res);
                view.getComponent("reliveView").setCallBack(() => {
                    Tool.loadPrefab("prefabs/gameLose", (res) => {
                        let view = cc.instantiate(res);
                        view.parent = cc.find("Canvas");
                    })
                });
                view.parent = cc.find("Canvas");
            });

        });
        if (this.openTitleClick) {
            cc.game.on("clickTitle", () => {
                this.openTitleClick = false;
                if (sceneMediator.getShowScene() == 7) {
                    cc.find("Canvas/key").active = true;
                    cc.game.emit("setTitle");
                } else {

                    if(this.toolEvent.length != 0){
                        cc.Component.EventHandler.emitEvents(this.toolEvent, []);
                    }
                }
            })
        }

        if (this.openPauseAction) {
            cc.game.on("clickPause", () => {
                if(this.toolEvent.length != 0){
                    cc.Component.EventHandler.emitEvents(this.toolEvent, []);
                }
            })
        }
    }

    protected onDestroy(): void {
        cc.game.off("receiveKeyEvent");
        cc.game.off("gameWin");
        cc.game.off("gameLose");
        if (this.openTitleClick) {
            cc.game.off("clickTitle");
        }
        if (this.openPauseAction) {
            cc.game.off("clickPause");
        }
    }

    pressKeyAction() {
        cc.Component.EventHandler.emitEvents(this.toolEvent, []);
    }

    shieldKeyBoard() {
        cc.game.emit("shieldPressKey", true);
    }

    unshieldKeyBoard() {
        cc.game.emit("shieldPressKey", false);
    }

    backToGame(event,data){

        if(parseInt(data) == 1){
            this.showNode[0].active = true;
        }

        if(parseInt(data) == 2){
            AudioManage.playSFX(soundList.switchRoom,false);
            this.closeVoiceType();
            cc.director.loadScene("game17");
            gameData.backToGame = true;
        }

        if(parseInt(data) == 3){
            this.showNode[0].active = false;
        }
    }
}
